package com.itheima.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.R;
import com.itheima.api.PmsProductApi;
import com.itheima.api.PmsProductCategoryApi;
import com.itheima.api.PmsProductCategoryAttributeRelationApi;
import com.itheima.dto.PmsProductCategoryParam;
import com.itheima.dto.PmsProductCategoryWithChildrenItem;
import com.itheima.pojo.db.PmsProduct;
import com.itheima.pojo.db.PmsProductCategory;
import com.itheima.pojo.db.PmsProductCategoryAttributeRelation;
import com.itheima.vo.PageResult;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chensen
 * @create 2022/10/13/20:30
 */
@Service
public class PmsProductCategoryService {
    @DubboReference
    private PmsProductCategoryApi pmsProductCategoryApi;
    @DubboReference
    private PmsProductCategoryAttributeRelationApi pmsProductCategoryAttributeRelationApi;
    @DubboReference
    private PmsProductApi pmsProductApi;
    public R create(PmsProductCategoryParam productCategoryParam) {
        PmsProductCategory productCategory = new PmsProductCategory();
        productCategory.setProductCount(0);
        BeanUtils.copyProperties(productCategoryParam, productCategory);

        this.setCategoryLevel(productCategory);
        pmsProductCategoryApi.insert(productCategory);

        List<Long> productAttributeIdList = productCategoryParam.getProductAttributeIdList();
        if (!CollectionUtils.isEmpty(productAttributeIdList)) {
            insertRelationList(productCategory.getId(), productAttributeIdList);
        }
        return R.success("添加成功");
    }

    private void insertRelationList(Long productCategoryId, List<Long> productAttributeIdList) {
        List<PmsProductCategoryAttributeRelation> relationList = new ArrayList<>();
        for (Long proAttId : productAttributeIdList) {
            PmsProductCategoryAttributeRelation relation = new PmsProductCategoryAttributeRelation();
            relation.setProductAttributeId(proAttId);
            relation.setProductCategoryId(productCategoryId);
            relationList.add(relation);
        }
        pmsProductCategoryAttributeRelationApi.insertList(relationList);
    }

    private void setCategoryLevel(PmsProductCategory productCategory) {
        if (productCategory.getParentId() == 0) {
            productCategory.setLevel(0);
        } else {
            PmsProductCategory pmsProductCategory = pmsProductCategoryApi.selectOne(productCategory.getParentId());
            if (pmsProductCategory != null) {
                productCategory.setLevel(pmsProductCategory.getLevel() + 1);
            } else {
                productCategory.setLevel(0);
            }
        }
    }

    public R delete(Long id) {
        pmsProductCategoryApi.delete(id);
        return R.success("删除成功");
    }

    public R listWithChildren() {
        List<PmsProductCategory> list = pmsProductCategoryApi.selectList();
        List<PmsProductCategoryWithChildrenItem> result = list.stream().map(item -> {
            PmsProductCategoryWithChildrenItem itemWC = new PmsProductCategoryWithChildrenItem();
            BeanUtils.copyProperties(item, itemWC);
            this.childList(itemWC);
            return itemWC;
        }).collect(Collectors.toList());
        return R.success(result);
    }

    private void childList(PmsProductCategoryWithChildrenItem itemWC) {
        List<PmsProductCategory> list = pmsProductCategoryApi.selectByPid(itemWC.getId());
        if (list.isEmpty()) {
            return;
        }

        List<PmsProductCategoryWithChildrenItem> collect = list.stream().map(item -> {
            PmsProductCategoryWithChildrenItem childList = new PmsProductCategoryWithChildrenItem();
            BeanUtils.copyProperties(item, childList);
            this.childList(childList);
            return childList;
        }).collect(Collectors.toList());
        itemWC.setChildren(collect);
    }

    public R getList(Long parentId, Integer pageNum, Integer pageSize) {
        IPage<PmsProductCategory> page = new Page<>(pageNum, pageSize);
        IPage<PmsProductCategory> pageL = pmsProductCategoryApi.selectPage(page, parentId);
        return R.success(PageResult.init(pageL));
    }

    public R updateNavStatus(List<Long> ids, Integer navStatus) {
        PmsProductCategory productCategory = new PmsProductCategory();
        productCategory.setNavStatus(navStatus);
        int count = pmsProductCategoryApi.update(productCategory, ids);
        if (count > 0) {
            return R.success("修改成功!");
        }
        return R.error("修改失败！");
    }

    public R updateShowStatus(List<Long> ids, Integer showStatus) {
        PmsProductCategory productCategory = new PmsProductCategory();
        productCategory.setShowStatus(showStatus);
        int count = pmsProductCategoryApi.update(productCategory, ids);
        if (count > 0) {
            return R.success("修改成功!");
        }
        return R.error("修改失败！");
    }

    public R update(Long id, PmsProductCategoryParam pmsProductCategoryParam) {
        PmsProductCategory productCategory = new PmsProductCategory();
        productCategory.setId(id);
        BeanUtils.copyProperties(pmsProductCategoryParam, productCategory);
        this.setCategoryLevel(productCategory);
        PmsProduct product = new PmsProduct();
        product.setProductCategoryName(productCategory.getName());
        pmsProductApi.update(product, id);

        if (!CollectionUtils.isEmpty(pmsProductCategoryParam.getProductAttributeIdList())) {
            pmsProductCategoryAttributeRelationApi.delete(id);
            insertRelationList(id, pmsProductCategoryParam.getProductAttributeIdList());
        } else {
            pmsProductCategoryAttributeRelationApi.delete(id);
        }

        pmsProductCategoryApi.updateById(productCategory);
        return R.success("修改成功");
    }

    public R getItem(Long id) {
        PmsProductCategory pmsProductCategory = pmsProductCategoryApi.select(id);
        return R.success(pmsProductCategory);
    }
}
